<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="1 死锁什么是死锁&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;通俗的讲，就是两个或多个进程无限期的阻塞、相互等待的一种状态。比如哲学家进餐问题。">
<meta property="og:type" content="article">
<meta property="og:title" content="死锁-死锁预防、死锁避免（资源分配图与银行家算法）、死锁检测、死锁解除">
<meta property="og:url" content="http://yoursite.com/2020/06/06/ttt/index.html">
<meta property="og:site_name" content="丛同学已经准备就绪">
<meta property="og:description" content="1 死锁什么是死锁&amp;ensp;&amp;ensp;&amp;ensp;&amp;ensp;通俗的讲，就是两个或多个进程无限期的阻塞、相互等待的一种状态。比如哲学家进餐问题。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-06-06T14:53:35.000Z">
<meta property="article:modified_time" content="2020-07-09T14:59:34.329Z">
<meta property="article:author" content="丛同学">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/06/06/ttt/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>死锁-死锁预防、死锁避免（资源分配图与银行家算法）、死锁检测、死锁解除 | 丛同学已经准备就绪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">丛同学已经准备就绪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">http://chieflion.gitee.io/</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/06/06/ttt/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="丛同学">
      <meta itemprop="description" content="热爱可抵岁月满长">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="丛同学已经准备就绪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          死锁-死锁预防、死锁避免（资源分配图与银行家算法）、死锁检测、死锁解除
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-06-06 22:53:35" itemprop="dateCreated datePublished" datetime="2020-06-06T22:53:35+08:00">2020-06-06</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-09 22:59:34" itemprop="dateModified" datetime="2020-07-09T22:59:34+08:00">2020-07-09</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/uncategorized/" itemprop="url" rel="index"><span itemprop="name">uncategorized</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="1-死锁"><a href="#1-死锁" class="headerlink" title="1 死锁"></a>1 死锁</h2><p><strong>什么是死锁</strong><br>&ensp;&ensp;&ensp;&ensp;通俗的讲，就是两个或多个进程无限期的阻塞、相互等待的一种状态。比如哲学家进餐问题。</p>
<a id="more"></a>
<p><strong>死锁产生的四个必要条件</strong><br>&ensp;&ensp;&ensp;&ensp;互斥：至少有一个资源必须属于非共享模式，即资源一次只能被一个进程使用；<br>&ensp;&ensp;&ensp;&ensp;占有并等待：进程自己有一部分资源，又想得到别人占有的资源；<br>&ensp;&ensp;&ensp;&ensp;非抢占：进程不能被抢占，即资源只能被进程在完成任务后自愿释放<br>&ensp;&ensp;&ensp;&ensp;循环等待：若干进程之间形成一种头尾相接的环形等待资源关系 ；<br>&ensp;&ensp;&ensp;&ensp;解决死锁的基本方法主要有 预防死锁、避免死锁、检测死锁、解除死锁 、鸵鸟策略 等。</p>
<h2 id="2-死锁预防"><a href="#2-死锁预防" class="headerlink" title="2 死锁预防"></a>2 死锁预防</h2><p>&ensp;&ensp;&ensp;&ensp;基本思想就是：打破死锁产生的四个必要条件之一。<br>    &ensp;&ensp;&ensp;&ensp;<strong>打破互斥条件</strong>：允许进程同时访问某些资源。但是，有些资源是不能被多个进程所共享的，这是由资源本身属性所决定的，因此，这种办法通常并无实用价值。<br>&ensp;&ensp;&ensp;&ensp; <strong>打破占有并等待条件</strong>：可以实行资源预先分配策略(进程在运行前<strong>一次性向系统申请它所需要的全部资源</strong>，若所需全部资源得不到满足，则不分配任何资源，此进程暂不运行；只有当系统能满足当前进程所需的全部资源时，才一次性将所申请资源全部分配给该线程)或者只允许进程在没有占用资源时才可以申请资源（一个进程可申请一些资源并使用它们，但是在当前进程<strong>申请更多资源之前，它必须全部释放当前所占有的资源</strong>）。但是这种策略也存在一些缺点：在很多情况下，无法预知一个进程执行前所需的全部资源，因为进程是动态执行的，不可预知的；同时，会降低资源利用率，导致降低了进程的并发性。<br>   &ensp;&ensp;&ensp;&ensp; <strong>打破非抢占条件</strong>：允许进程强行从占有者哪里夺取某些资源。这种预防死锁的方式实现起来困难，会降低系统性能。<br>   &ensp;&ensp;&ensp;&ensp; <strong>打破循环等待条件</strong>：实行资源有序分配策略。对所有资源排序编号，所有进程对资源的请求必须严格按资源序号递增的顺序提出，即只有占用了小号资源才能申请大号资源，这样就不回产生环路，预防死锁的发生。</p>
<h2 id="3-死锁避免"><a href="#3-死锁避免" class="headerlink" title="3 死锁避免"></a>3 死锁避免</h2><p>  &ensp;&ensp;&ensp;&ensp; 资源分配图算法和银行家算法是两种经典的死锁避免的算法。<br>  <strong>银行家算法</strong><br>  <a href="https://www.bilibili.com/video/BV1kb41147CT?from=search&seid=310228432549551990" target="_blank" rel="noopener">B站视频</a><br>  &ensp;&ensp;&ensp;&ensp;银行家算法是一种经典的死锁避免算法，银行家相当于操作系统，资金是资源，进程是客户，银行家算法是一种最有代表性的避免死锁的算法。在避免死锁方法中允许进程动态地申请资源，但系统在进行资源分配之前，应先计算此次分配资源的安全性，若分配不会导致系统进入不安全状态，则分配，否则等待。为实现银行家算法，系统必须设置若干数据结构。</p>
<p>安全序列是指一个进程序列{P1，…，Pn}是安全的，即对于每一个进程Pi(1≤i≤n），它以后尚需要的资源量不超过系统当前剩余资源量与所有进程Pj (j &lt; i )当前占有资源量之和。（即在分配过程中，不会出现某一进程后续需要的资源量比其他所有进程及当前剩余资源量总和还大的情况）</p>
<h2 id="4-死锁检测"><a href="#4-死锁检测" class="headerlink" title="4 死锁检测"></a>4 死锁检测</h2><p> &ensp;&ensp;&ensp;&ensp;  <strong>使用JConsole检测死锁</strong>：JDK提供的虚拟机监视工具，（相对起来图形化）。<br>  &ensp;&ensp;&ensp;&ensp; <strong>使用JStack检测死锁</strong>：JDK提供的命令行工具。</p>
<h2 id="5-死锁解除"><a href="#5-死锁解除" class="headerlink" title="5 死锁解除"></a>5 死锁解除</h2><p>　 &ensp;&ensp;&ensp;&ensp;　<strong>死锁解除的常用两种方法为进程终止和资源抢占</strong>。所谓进程终止是指简单地终止一个或多个死锁进程以打破循环等待；所谓资源抢占是指从一个或多个死锁进程那里抢占一个或多个资源。
　</p>
<h2 id="6-参考文献"><a href="#6-参考文献" class="headerlink" title="6 参考文献"></a>6 参考文献</h2><p><a href="https://blog.csdn.net/justloveyou_/article/details/78304294?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task" target="_blank" rel="noopener">参考文献1</a><br><a href="https://blog.csdn.net/wolfcode_cn/article/details/80728473" target="_blank" rel="noopener">参考文献2 </a></p>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item"></div>
      <div class="post-nav-item">
    <a href="/2020/06/21/weifuwu/" rel="next" title="单机、集群、分布式和微服务等相关概念解析以及Redis实现分布式锁">
      单机、集群、分布式和微服务等相关概念解析以及Redis实现分布式锁 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-死锁"><span class="nav-number">1.</span> <span class="nav-text">1 死锁</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-死锁预防"><span class="nav-number">2.</span> <span class="nav-text">2 死锁预防</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-死锁避免"><span class="nav-number">3.</span> <span class="nav-text">3 死锁避免</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-死锁检测"><span class="nav-number">4.</span> <span class="nav-text">4 死锁检测</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-死锁解除"><span class="nav-number">5.</span> <span class="nav-text">5 死锁解除</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-参考文献"><span class="nav-number">6.</span> <span class="nav-text">6 参考文献</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">丛同学</p>
  <div class="site-description" itemprop="description">热爱可抵岁月满长</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">6</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">丛同学</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
